<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="项目标段" prop="institutionCode">
        <el-select
          v-model="queryParams.institutionCode"
          placeholder="请选择"
          size="small"
          style="width: 240px" @change="handleBidChange"
        >
          <el-option-group
            v-for="group in projectBidOptions"
            :key="group.name"
            :label="group.name">
            <el-option
              v-for="item in group.bids"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-option-group>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="idNo">
        <el-input
          v-model="queryParams.idNo"
          placeholder="身份证号/姓名"
          clearable
          size="small"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="在岗状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择在岗状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.labour_staff_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" plain icon="el-icon-search" @click="moreVisible = true" size="mini" >更多</el-button>
      </el-form-item>

      <div>
        <el-drawer
          title="更多查询条件"
          :visible.sync="moreVisible"
          size="40%"
          direction="rtl"
        >
          <div style="padding-top: 5px;">
            <el-row>
              <el-col :span="12">
                <el-form-item label="性　　别" prop="sex">
                  <el-select v-model="queryParams.sex" clearable placeholder="请选择性别" style="width: 250px;">
                    <el-option
                      v-for="dict in dict.type.sys_user_sex"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="民　　族" prop="nation">
                  <el-select
                    v-model="queryParams.nation"
                    clearable
                    placeholder="请选择民族"
                    style="width: 240px;">
                    <el-option
                      v-for="item in nationOptions"
                      :key="item.name"
                      :label="item.name"
                      :value="item.name"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="籍　　贯">
                  <el-select v-model="queryParams.nativePlace" clearable placeholder="请选择籍贯" style="width: 250px;">
                    <el-option
                      v-for="item in provinceOptions"
                      :key="item.name"
                      :label="item.name"
                      :value="item.name"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="学　　历">
                  <el-select v-model="queryParams.degreeName" clearable placeholder="请选择学历" style="width: 250px;">
                    <el-option
                      v-for="dict in dict.type.sys_degree_name"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="年　　龄">
                  <el-input type="number" v-model="queryParams.params.age1" clearable placeholder=">=此值" style="width: 119px;"/> -
                  <el-input type="number" v-model="queryParams.params.age2" clearable placeholder="<=此值" style="width: 119px;"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="政治面貌">
                  <el-select v-model="queryParams.politicalStatus" clearable placeholder="请选择政治面貌" style="width: 250px;">
                    <el-option
                      v-for="dict in dict.type.sys_political_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="施工队伍">
                  <el-select clearable filterable
                             v-model="queryParams.battalionId"
                             placeholder="请选择施工队伍"
                             style="width: 250px;">
                    <el-option
                      v-for="item in battalionOptions"
                      :key="item.battalionId"
                      :label="item.battalionName"
                      :value="item.battalionId"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="施工班组">
                  <el-select clearable filterable
                             v-model="queryParams.teamId"
                             placeholder="请选择施工班组"
                             style="width: 250px;">
                    <el-option
                      v-for="item in teamOptions"
                      :key="item.teamId"
                      :label="item.teamName"
                      :value="item.teamId"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="岗位工种">
                  <el-select clearable filterable
                             v-model="queryParams.postId"
                             placeholder="请选择岗位工种"
                             style="width: 250px;">
                    <el-option
                      v-for="item in postOptions"
                      :key="item.postId"
                      :label="item.postName"
                      :value="item.postId"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="班组长">
                  <el-select v-model="queryParams.isMonitor" clearable placeholder="请选择班组长" style="width: 250px;">
                    <el-option
                      v-for="dict in dict.type.sys_yes_no"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="进场日期">
                  <el-date-picker
                    v-model="enterDateRange"
                    value-format="yyyy-MM-dd"
                    type="daterange"
                    style="width: 250px;"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="退场日期">
                  <el-date-picker
                    v-model="exitDateRange"
                    value-format="yyyy-MM-dd"
                    type="daterange"
                    style="width: 250px;"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <div slot="footer" class="dialog-footer" style="display:flex;align-items:center;justify-content:center">
              <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">查询</el-button>
              <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
            </div>
          </div>
        </el-drawer>
      </div>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-dropdown @command="handleIdNo">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload"
            size="mini"
            v-hasPermi="['labour:staff:add']">
            上传身份证
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="1">单个</el-dropdown-item>
            <el-dropdown-item command="2">批量</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['labour:staff:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-dropdown @command="handleImport">
          <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            v-hasPermi="['labour:staff:import']"
          >导入
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="1">基本信息</el-dropdown-item>
            <el-dropdown-item command="2">进场信息</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleBatchExit"
          v-hasPermi="['labour:staff:exit']"
        >批量退场</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['labour:staff:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="staffList" :height="tableHeight" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"  />
      <el-table-column min-width="55" align="center" prop="photo" label="姓名身份证">
        <template slot-scope="scope">
          <img v-if="scope.row.photo==''|| scope.row.photo==undefined" :src="require('@/assets/images/profile.jpg')" style="width: 100px; height: 100px; margin-bottom:-4px"/>
          <img v-else :src="base+scope.row.photo" style="width: 100px; height: 100px; margin-bottom:-4px"/>
        </template>
      </el-table-column>
      <el-table-column label="" align="left" prop="idNo" width="180px;">
        <template slot-scope="scope">
          <el-link :underline="false" type="primary" @click="handleBrowse(scope.row)">{{ scope.row.name }}</el-link>
          <p>{{ scope.row.idNo }}</p>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="施工队伍" align="center" prop="battalionName" />
      <el-table-column label="施工班组" align="center" prop="teamName" />
      <el-table-column label="岗位工种" align="center" prop="postName" />
      <el-table-column label="进退场日期" align="center">
        <template slot-scope="scope">
          <p style="height: 8px;">{{ parseTime(scope.row.enterDate, '{y}-{m}-{d}') }}</p>
          <p style="height: 8px;">-</p>
          <p style="height: 8px;">{{ parseTime(scope.row.exitDate, '{y}-{m}-{d}') }}</p>
        </template>
      </el-table-column>
      <el-table-column label="在岗状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.labour_staff_status" :value="scope.row.status"/>
          <el-button
            size="mini"
            type="success"
            v-if="scope.row.status == '1' || scope.row.status == '3'"
            @click="handleEnter(scope.row)"
            v-hasPermi="['labour:staff:enter']"
          >进场</el-button>
          <el-button
            size="mini"
            type="danger"
            v-else
            @click="handleExit(scope.row)"
            v-hasPermi="['labour:staff:exit']"
          >退场</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-upload"
            v-if="scope.row.uploadCard == 'N'"
            @click="handleRowIdNo(scope.row)"
            v-hasPermi="['labour:staff:add']"
          >上传身份证</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['labour:staff:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['labour:staff:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" :close-on-click-modal="false" style="margin-top: 150px" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?institutionCode=' + this.queryParams.institutionCode"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加劳务工对话框 -->
    <add ref="add"></add>

    <!-- 查看劳务工对话框 -->
    <browse ref="browse"></browse>

    <!-- 上传身份证（单个） -->
    <card-upload ref="cardUpload"></card-upload>

    <!-- 上传身份证（批量） -->
    <card-zip-upload ref="cardZip"></card-zip-upload>

    <!-- 导入结果对话框 -->
    <el-dialog :title="result.title" :visible.sync="result.open" :close-on-click-modal="false" width="600px" append-to-body>
      <el-form label-width="68px">
        <el-form-item label="状态">
          <el-select v-model="result.select" placeholder="请选择" @change="handleResultOnChange">
            <el-option
              v-for="item in result.options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-table :data="result.rows" :height="resultTableHeight" highlight-current-row>
        <el-table-column label="行号" align="center" prop="row" />
        <el-table-column label="内容" align="center" prop="content" width="400" show-overflow-tooltip />
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-button size="mini" v-if="scope.row.status=='success'" type="success" icon="el-icon-check" circle></el-button>
            <el-button size="mini" v-if="scope.row.status=='error'" type="danger" icon="el-icon-close" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="result.open = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 劳务工进场 -->
    <el-dialog :title="enter.title" :visible.sync="enter.open" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="enterForm" :model="enter.form" :rules="enter.rules" label-width="80px">
        <el-form-item label="姓　　名" prop="name">
          <el-input v-model="enter.form.name" :disabled="true"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNo">
          <el-input v-model="enter.form.idNo" :disabled="true" />
        </el-form-item>
        <el-form-item label="进场日期" prop="enterDate">
          <el-date-picker
            v-model="enter.form.enterDate"
            align="right"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期" style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="施工队伍" prop="battalionId">
          <el-select
            v-model="enter.form.battalionId"
            placeholder="请选择"
            style="width: 100%;">
            <el-option
              v-for="item in enter.battalionOptions"
              :key="item.battalionId"
              :label="item.battalionName"
              :value="item.battalionId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="施工班组" prop="teamId">
          <el-select
            v-model="enter.form.teamId"
            placeholder="请选择"
            style="width: 100%;">
            <el-option
              v-for="item in enter.teamOptions"
              :key="item.teamId"
              :label="item.teamName"
              :value="item.teamId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位工种" prop="postId">
          <el-select
            v-model="enter.form.postId"
            placeholder="请选择"
            style="width: 100%;">
            <el-option
              v-for="item in enter.postOptions"
              :key="item.postId"
              :label="item.postName"
              :value="item.postId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班组长" prop="isMonitor" >
          <el-select v-model="enter.form.isMonitor" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEnterForm">确 定</el-button>
        <el-button @click="cancelEnter">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 劳务工退场 -->
    <el-dialog :title="exit.title" :visible.sync="exit.open" :close-on-click-modal="false" style="margin-top: 150px" width="500px" append-to-body>
      <el-form ref="exitForm" :model="exit.form" :rules="exit.rules" label-width="80px">

        <el-form-item label="退场日期" prop="exitDate">
          <el-date-picker
            v-model="exit.form.exitDate"
            align="right"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期" style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitExitForm">确 定</el-button>
        <el-button @click="cancelExit">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    listStaff,
    delStaff,
    exportStaff,
    beforeEnterValidate,
    enterStaff,
    exitStaff,
    batchExitStaff,
    initSearchSelect
  } from "@/api/site/labour/staff";
import { selectProjectBidBranch } from "@/api/project/bid";
import Add from "@/views/site/labour/staff/add";
import Browse from "@/views/site/labour/staff/browse";
import CardUpload from "@/views/site/labour/staff/cardUpload";
import CardZipUpload from "@/views/site/labour/staff/cardZipUpload";
import { getToken } from "@/utils/auth";

export default {
  name: "Staff",
  dicts: ['sys_user_sex', 'labour_staff_status', 'sys_yes_no'],
  components: { CardUpload, Add, CardZipUpload, Browse },
  data() {
    return {
      base: process.env.VUE_APP_BASE_API,
      tableHeight: window.innerHeight - 285,
      resultTableHeight: window.innerHeight - 400,
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      institutionCode: undefined,
      // 项目标段分组下拉框
      projectBidOptions: [],
      battalionOptions: [],
      teamOptions: [],
      postOptions: [],
      nationOptions: [],
      provinceOptions: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 农民工花名册表格数据
      staffList: [],
      // 查询参数
      moreVisible: false,
      enterDateRange: [],
      exitDateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        institutionCode: undefined,
        idNo: undefined,
        status: undefined,
        battalionId: undefined,
        teamId: undefined,
        postId: undefined,
        sex: '',
        nation: '',
        nativePlace: '',
        degreeName: '',
        politicalStatus: '',
        isMonitor: '',
        params: {}
      },
      // 用户导入参数
      upload: {
        type: "1",
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { token: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/labour/staff/importBasicData"
      },
      // 导入结果参数
      result: {
        open: false,
        title: "",
        rows: [],
        temp: [],
        select: 'error',
        options: [{
          value: 'all',
          label: '全部'
        },{
          value: 'error',
          label: '异常'
        },{
          value: 'success',
          label: '正常'
        }]
      },
      enter: {
        open: false,
        title: "",
        form: {},
        battalionOptions: [],
        teamOptions:[],
        postOptions:[],
        rules: {
          enterDate: [
            { required: true, message: "进场日期不能为空", trigger: "blur" }
          ],
          battalionId: [
            { required: true, message: "施工队伍不能为空", trigger: "blur" }
          ],
          teamId: [
            { required: true, message: "施工班组不能为空", trigger: "blur" }
          ],
          postId: [
            { required: true, message: "岗位工种不能为空", trigger: "blur" }
          ],
          isMonitor: [
            { required: true, message: "班组长不能为空", trigger: "blur" }
          ],
        }
      },
      exit: {
        type: 0,
        open: false,
        title: "",
        form: {},
        rules: {
          exitDate: [
            { required: true, message: "退场日期不能为空", trigger: "blur" }
          ]
        }
      }
    };
  },
  created() {
    this.initBidSelect();
  },
  methods: {
    /** 初始化标段下拉框 */
    initBidSelect() {
      selectProjectBidBranch().then(response => {
        this.projectBidOptions = response.data;
        if (this.projectBidOptions.length > 0) {
          this.institutionCode = this.projectBidOptions[0].bids[0].code;
          this.queryParams.institutionCode = this.projectBidOptions[0].bids[0].code;
          this.getList();
          this.initSearchSelect();
        }
      });
    },
    initSearchSelect() {
      initSearchSelect({institutionCode: this.queryParams.institutionCode}).then(response => {
        this.battalionOptions = response.battalions;
        this.teamOptions = response.teams;
        this.postOptions = response.posts;
        this.nationOptions = response.nations;
        this.provinceOptions = response.provinces;
      });
    },
    // 标段下拉框发生变化
    handleBidChange() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.initSearchSelect();
    },
    /** 查询农民工花名册列表 */
    getList() {
      this.loading = true;
      listStaff(this.queryParams).then(response => {
        this.staffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查看人员详情
    handleBrowse(row) {
      this.$router.push({ path: '/site/labour/staff/view/' + row.id, query: { id: row.id, idNo: row.idNo} })
    },
    // 身份证上传按钮
    handleIdNo(command) {
      console.log(command);
      if (command == '1') {
        this.$refs.cardUpload.show(this.queryParams.institutionCode, 0);
      } else {
        this.$refs.cardZip.show(this.queryParams.institutionCode);
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.addDateRange(this.queryParams, this.enterDateRange, 'EnterDate');
      this.addDateRange(this.queryParams, this.exitDateRange, 'ExitDate');
      console.log(this.queryParams);
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.enterDateRange = [];
      this.exitDateRange = [];
      this.queryParams = {
        sex: '',
        nation: '',
        nativePlace: '',
        degreeName: '',
        age1: undefined,
        age2: undefined,
        politicalStatus: '',
        battalionId: '',
        teamId: '',
        postId: '',
        isMonitor: '',
      }
      this.resetForm("queryForm");
      this.queryParams.institutionCode = this.institutionCode;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.add.show(this.queryParams.institutionCode, 0);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.add.show(this.queryParams.institutionCode, row.id);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delStaff(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport(command) {
      this.result.rows = [];
      this.result.temp = [];
      this.result.select = 'error';
      this.upload.type = command;
      if (this.upload.type == '1') {
        this.upload.title = "劳务工基本信息导入";
        this.upload.url = process.env.VUE_APP_BASE_API + "/labour/staff/importBasicData";
        this.upload.open = true;
      } else if (this.upload.type == '2') {
        this.upload.title = "劳务工进场信息导入";
        this.upload.url = process.env.VUE_APP_BASE_API + "/labour/staff/importEnterData";
        this.upload.open = true;
      }
    },
    /** 下载模板操作 */
    importTemplate() {
      if (this.upload.type == '1') {
        this.$download.staticExcelTemplate("/static/excel/staff_basic_template.xlsx","劳务工花名册模板.xlsx");
      } else if (this.upload.type == '2') {
        this.$download.staticExcelTemplate("/static/excel/staff_enter_template.xlsx","劳务工进场模板.xlsx");
      }
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.$loading().close();
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      if (response.rows != undefined) {
        this.result.temp = response.rows;
        response.rows.forEach(e => {
          if (e.status == 'error') {
            this.result.rows.push(e);
          }
        });
      }
      if (response.status == "success") {
        this.getList();
      }
      this.$confirm(response.msg, '导入结果', {
        showCancelButton: true,
        showConfirmButton: response.rows != undefined,
        confirmButtonText: '详情',
        cancelButtonText: '取消',
        type: response.status
      }).then(() => {
        this.result.title = "查看导入详情";
        this.result.open = true;
      }).catch(() => {
      });
    },
    // 导入结果状态发生改变
    handleResultOnChange() {
      if (this.result.select == 'all') {
        this.result.rows = this.result.temp;
      } else if (this.result.select == 'error') {
        this.result.rows = [];
        this.result.temp.forEach(e => {
          if (e.status == 'error') {
            this.result.rows.push(e);
          }
        });
      } else if (this.result.select == 'success') {
        this.result.rows = [];
        this.result.temp.forEach(e => {
          if (e.status == 'success') {
            this.result.rows.push(e);
          }
        });
      }
    },
    // 提交上传文件
    submitFileForm() {
      this.$loading({
        lock: true,
        text: '数据导入中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      this.$refs.upload.submit();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有劳务工花名册数据项？').then(() => {
        this.exportLoading = true;
        return exportStaff(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    // 进场按钮操作
    handleEnter(row) {
      this.resetEnter();
      beforeEnterValidate(row.id).then(res => {
        this.enter.form.name = res.name;
        this.enter.form.idNo = res.idNo;
        this.enter.battalionOptions = res.battalions;
        this.enter.teamOptions = res.teams;
        this.enter.postOptions = res.posts;
        this.enter.form.staffId = row.id;
        this.enter.open = true;
        this.enter.title = "劳务工进场";
      });
    },
    // 取消按钮
    cancelEnter() {
      this.enter.open = false;
      this.resetEnter();
    },
    // 表单重置
    resetEnter() {
      this.enter.form = {
        id: null,
        staffId: null,
        idNo: null,
        institutionCode: null,
        enterDate: null,
        exitDate: null,
        battalionId: null,
        teamId: null,
        postId: null,
        isMonitor: 'N',
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("enterForm");
    },
    /** 进场提交按钮 */
    submitEnterForm() {
      this.$refs["enterForm"].validate(valid => {
        if (valid) {
          enterStaff(this.enter.form).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.enter.open = false;
            this.getList();
          });
        }
      });
    },
    // 退场按钮操作
    handleExit(row) {
      this.exit.type = 1;
      this.resetExit();
      this.exit.form.staffId = row.id;
      this.exit.open = true;
      this.exit.title = "劳务工退场";
    },
    // 批量退场
    handleBatchExit() {
      const ids = this.ids;
      let flag = false;
      let msg = "您所选的";
      this.staffList.forEach(e => {
        for (let i = 0; i < ids.length; i++) {
          if (e.id == ids[i] && e.status != '2') {
            msg += e.name + ",";
            flag = true;
          }
        }
      });
      if (flag) {
        msg = msg.substring(0, msg.length - 1);
        msg += "不是已进场人员，不能进行退场操作。";
        this.$modal.msgError(msg);
      } else {
        this.exit.type = 2;
        this.exit.open = true;
        this.exit.title = "劳务工退场";
      }
    },
    // 取消按钮
    cancelExit() {
      this.exit.open = false;
      this.resetExit();
    },
    // 表单重置
    resetExit() {
      this.exit.form = {
        staffId: null,
        exitDate: null,
      };
      this.resetForm("exitForm");
    },
    /** 退场提交按钮 */
    submitExitForm() {
      this.$refs["exitForm"].validate(valid => {
        if (valid) {
          console.log(this.exit.type);
          if (this.exit.type == 1) {
            exitStaff(this.exit.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.exit.open = false;
              this.getList();
            });
          } else {
            const ids = this.ids.join(",");
            batchExitStaff({ids: ids, exitDate: this.exit.form.exitDate}).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.exit.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 行上传身份证按钮
    handleRowIdNo(row) {
      this.$refs.cardUpload.show(this.queryParams.institutionCode, row.id);
    }
  }
};
</script>
<style lang="scss" scoped>

</style>
